﻿<ComClass(ComImpersonate.ClassId, ComImpersonate.InterfaceId, ComImpersonate.EventsId)> _
Public Class ComImpersonate

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "bd56b7c7-877c-494a-a8e6-4444403de7ce"
    Public Const InterfaceId As String = "bd45f842-8825-4295-a5d7-7eca75c841ed"
    Public Const EventsId As String = "2e3ef221-3856-4e07-b9bc-0be7e917d41f"
#End Region

    '*********************************************************************************************************************
    '
    '             Class:  ComImpersonate
    '      Organization:  Indiana University Foundation        
    '      Initial Date:  02/13/2008
    '      Last Updated:  02/13/2008
    '     Programmer(s):  Blake Pell
    '       Description:  Exposing the Iuf frameworks implementation of Win32 impersonation so that Com 
    '                     enabled languages such as Classic ASP can call our .Net libraries.  In short, this is a 
    '                     wrapper for Iuf.Network.Authentication.Impersonation.
    '      Dependencies:  None
    '             Usage:  See documentation.
    '                     
    '*********************************************************************************************************************

    Private _oAuth As New Impersonation()

    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub Impersonate(ByVal Username As String, ByVal Password As String, ByVal Domain As String)

        If _oAuth.IsImpersonating = True Then
            Call UndoImpersonation()
        End If

        If _oAuth Is Nothing Then
            _oAuth = New Iuf.Network.Authentication.Impersonation()
        End If

        _oAuth.Username = Username
        _oAuth.Password = Password
        _oAuth.Domain = Domain
        _oAuth.ImpersonateUser()
    End Sub

    Public Sub UndoImpersonation()
        _oAuth.UndoImpersonation()
        _oAuth = Nothing
    End Sub

    Public Function GetCurrentUsername() As String

        If _oAuth Is Nothing Then
            Return ""
        End If

        Return _oAuth.GetCurrentUsername()
    End Function

End Class


